﻿using System;
using System.Linq;
using System.Windows;
using YwangHRMSys.DAL;
using YwangHRMSys.DBUtility;
using YwangHRMSys.Model;


namespace YwangHRMSys.UI.LoginMgr
    {
        /// <summary>
        ///     LoginWindowList.xaml 的交互逻辑
        /// </summary>
        public partial class LoginWindowList : Window
            {

                public LoginWindowList()
                {
                    InitializeComponent();
                }

                /// <summary>
                ///     自动加载操作员列表
                /// </summary>
                private void LoadDate()
                {
                    var Dal = new OperatorInfoDAL();

                    DataGridOperatorList.ItemsSource = Dal.ListAll();


                }

                private void Window_Loaded( object sender, RoutedEventArgs e )
                {
                    LoadDate();
                       

                }

                private void BtnAdd_Click( object sender, RoutedEventArgs e )
                {

                    var windowEdit = new LoginWindowEdit();

                    windowEdit.IsInsert = true;
                    if (windowEdit.ShowDialog() == true)
                        {
                            LoadDate();

                        }


                }

                private void BtnEdit_Click( object sender, RoutedEventArgs e )
                {
                    var opmModel = (OperatorInfoModel) DataGridOperatorList.SelectedItem;

                    if (opmModel == null)
                        {
                            MessageBox.Show("没有选中任何行");
                            return;
                        }
                    var edit = new LoginWindowEdit();
                    edit.IsInsert = false;
                    edit.EditnGuid = opmModel.Id;
                    if (edit.ShowDialog() == true)
                        {
                            LoadDate();

                        }

                }

                private void BtnDelete_Click( object sender, RoutedEventArgs e )
                {
                    var opModel = (OperatorInfoModel) DataGridOperatorList.SelectedItem;
                    if (opModel == null)
                        {
                            MessageBox.Show("没有选中任何行");
                            return;
                        }
                    if (MessageBox.Show("真的要删除" + opModel.UserName + "吗？", "提醒", MessageBoxButton.YesNo)
                        == MessageBoxResult.Yes)
                        {
                            var dal = new OperatorInfoDAL();
                            dal.DeleteById(opModel.Id);
                            LoadDate();
                        }

                }

                private void BtnLocked_Click( object sender, RoutedEventArgs e )
                {
                    var opModel = (OperatorInfoModel) DataGridOperatorList.SelectedItem;
                    if (opModel == null)
                        {
                            MessageBox.Show("没有选中任何行");
                            return;
                        }
                    Guid id = opModel.Id;

                    new OperatorInfoDAL().Locked(id);
                    LoadDate();
                }

                private void BtnUnLock_Click( object sender, RoutedEventArgs e )
                {
                    var opModel = (OperatorInfoModel) DataGridOperatorList.SelectedItem;
                    if (opModel == null)
                        {
                            MessageBox.Show("没有选中任何行");
                            return;
                        }
                    Guid id = opModel.Id;
                    new OperatorInfoDAL().UnLocked(id);
                    LoadDate();
                }

                private void BtcPassword_Click(object sender, RoutedEventArgs e)
                {
                    var opModel = (OperatorInfoModel)DataGridOperatorList.SelectedItem;
                    if (opModel == null)
                    {
                        MessageBox.Show("没有选中任何行");
                        return;
                    }
                    Guid id = opModel.Id;
                    const string newpassword = "000000";
                    string passWord = CommonHelper.GetMD5(newpassword + CommonHelper.GetPasswordSalt());
                    new OperatorInfoDAL().ResetPassword(id,passWord);
                    LoadDate();
                    MessageBox.Show(opModel.UserName+"的重置密码为6个0");
                }

                private void BtnPermissionAdd_Click(object sender, RoutedEventArgs e)
                {
                    var opModel = (OperatorInfoModel)DataGridOperatorList.SelectedItem;
                    if (opModel == null)
                    {
                        MessageBox.Show("没有选中任何行");
                        return;
                    }
                    var adminModel = new AdminPermissionModel();
                    adminModel.Id = opModel.Id;
                    adminModel.UserName = opModel.UserName;
                    adminModel.Permission = opModel.IsLocked;
                 
             new AdminPermissionDal().InsertAdminPermission(adminModel);
                    MessageBox.Show(adminModel.UserName+"权限提升为管理员");
                }

                private void BtnPermissionSub_Click(object sender, RoutedEventArgs e)
                {

             

                    var opModel = (OperatorInfoModel)DataGridOperatorList.SelectedItem;
                    if (opModel == null)
                    {
                        MessageBox.Show("没有选中任何行");
                        return;
                    }
                   Guid id=  opModel.Id;
                 var  model = new AdminPermissionDal().ListAll();


                 int i = model.Count();
                    if (i<=1)
                        {
                            MessageBox.Show(opModel.UserName + "已经是最后的管理员了。不能再降低权限了!");
           
                        }
                    else
                        {
                   new AdminPermissionDal().DeleteById(id);
                    MessageBox.Show(opModel.UserName + "权限降为普通操作员");
                        }

                 
                 
                }

            }
    }